package leetcode;

import java.util.HashSet;
import java.util.Set;

public class l0036_有效的数独_解法1_哈希表 {
    public boolean isValidSudoku(char[][] board) {
        Set<Character>[] rows = new HashSet[9];
        Set<Character>[] cols = new HashSet[9];
        Set<Character>[] boxes = new HashSet[9];
        for (int i = 0; i < 9; i++) {
            rows[i] = new HashSet<>();
            cols[i] = new HashSet<>();
            boxes[i] = new HashSet<>();
        }
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                char num = board[row][col];
                if (num == '.') {
                    continue;
                }
                if (rows[row].contains(num)) {
                    return false;
                }
                if (cols[col].contains(num)) {
                    return false;
                }
                int boxIndex = (row / 3) * 3 + col / 3;
                if (boxes[boxIndex].contains(num)) {
                    return false;
                }
                rows[row].add(num);
                cols[col].add(num);
                boxes[boxIndex].add(num);
            }
        }
        return true;
    }
}
